package org.ihelpuoo.lang;

public class ArrayUtils<T>
{
	public static int indexOf(int[] arr, int v, int offset, int len)
	{
		if (null == arr)
			return -1;
		for (int i = offset; i < len; i++)
			if (arr[i] == v)
				return i;
		return -1;
	}

	public static int indexOf(int[] arr, int v)
	{
		return indexOf(arr,v,0,arr.length);
	}

	public static boolean isInArray(int[] arr, int v)
	{
		return indexOf(arr,v) > -1;
	}

	public static int indexOf(boolean[] arr, boolean v)
	{
		if (null == arr || arr.length == 0)
			return -1;
		for (int i = 0; i < arr.length; i++)
			if (arr[i] == v)
				return i;
		return -1;
	}

	public static <T> int indexOf(T[] arr, T v, int off, int len)
	{
		if (null == arr)
			return -1;
		for (int i = off; i < len; i++)
			if (arr[i].equals(v))
				return i;
		return -1;
	}

	public static <T> int indexOf(T[] arr, T v)
	{
		return indexOf(arr,v,0,arr.length);
	}

	@SuppressWarnings("unchecked")
	public static Comparable maxInArray(Comparable[] arr)
	{
		if (null == arr)
			return null;
		if (arr.length == 1)
			return arr[0];
		Comparable m = arr[0];
		for (int i = 1; i < arr.length; i++)
			if (arr[i].compareTo(m) > 0)
				m = arr[i];
		return m;
	}

	@SuppressWarnings("unchecked")
	public static int maxIndexInArray(Comparable[] arr)
	{
		if (null == arr)
			return -1;
		if (arr.length == 1)
			return 0;
		int index = 0;
		for (int i = 1; i < arr.length; i++)
			if (arr[i].compareTo(arr[index]) > 0)
				index = i;
		return index;
	}

	@SuppressWarnings("unchecked")
	public static Comparable minInArray(Comparable[] arr)
	{
		if (null == arr)
			return null;
		if (arr.length == 1)
			return arr[0];
		Comparable m = arr[0];
		for (int i = 1; i < arr.length; i++)
			if (arr[i].compareTo(m) < 0)
				m = arr[i];
		return m;
	}

	@SuppressWarnings("unchecked")
	public static int minIndexInArray(Comparable[] arr)
	{
		if (null == arr)
			return -1;
		if (arr.length == 1)
			return 0;
		int index = 0;
		for (int i = 1; i < arr.length; i++)
			if (arr[i].compareTo(arr[index]) < 0)
				index = i;
		return index;
	}

	public static <T> boolean isInArray(T[] arr, T v)
	{
		return indexOf(arr,v) != -1;
	}

	public static <T> void copyRef(T[] src, int off, int len, T[] dest, int doff)
	{
		int max = off + len;
		for (int i = off; i < max; i++)
			dest[doff + i] = src[i];
	}

	public static <T> void copyRef(T[] src, T[] dest)
	{
		copyRef(src,0,src.length,dest,0);
	}

	public static <T> void copyRef(T[] src, int off, int len, T[] dest)
	{
		copyRef(src,off,len,dest);
	}

	@SuppressWarnings("unchecked")
	public static <T, E> void cast(T[] src, E[] dest)
	{
		if (null == src || null == dest)
			return;
		int max = Math.min(src.length,dest.length);
		for (int i = 0; i < max; i++)
			dest[i] = (E)src[i];
	}

	public static String toString(int[] a)
	{
		return toString(a,",");
	}

	public static String toString(int[] a, String delimiter)
	{
		if (null == a || a.length == 0)
			return null;
		StringBuffer sb = new StringBuffer();
		sb.append(a[0]);
		if (null != delimiter)
			for (int i = 1; i < a.length; i++)
			{
				sb.append(delimiter);
				sb.append(a[i]);
			}
		else
			for (int i = 1; i < a.length; i++)
				sb.append(a[i]);

		return sb.toString();
	}

	public static <T> String toString(T[] a)
	{
		return toString(a,",");
	}

	public static <T> String toString(T[] a, String delimiter)
	{
		if (null == a || a.length == 0)
			return null;
		StringBuffer sb = new StringBuffer();
		sb.append(a[0]);
		for (int i = 1; i < a.length; i++)
		{
			sb.append(delimiter);
			sb.append(a[i].toString());
		}
		return sb.toString();
	}
}
